// pages/release/release.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imageList: [],
        countIndex:8,//最多上传图片的数量
        count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        textValue:'',
        img2_list: [
            '/images/star-3.png',
            '/images/star-3.png',
            '/images/star-3.png',
            '/images/star-3.png',
            '/images/star-3.png'
        ],
        img3: '/images/star-5.png',
        star:0,
    },
    // 选择上传的图片
    chooseImage: function () {
      var self = this;
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          var tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: Host + 'mine/uploadFile',
            filePath: tempFilePaths[0],
            name: 'files',
            formData: {
              'host': Host
            },
            success: function (res) {
              var files = self.data.imageList
              files.push(res.data)
              self.setData({
                imageList: files
              })
            }
          })
        }
      })
    },
    // 放大图片
    previewImage: function (e) {
        var current = e.currentTarget.dataset.src;
        wx.previewImage({
            current: current,
            urls: this.data.imageList
        })
    },
    // 获取文本值
    getValue:function(e){
        console.log(e.detail.value)
        this.setData({
            textValue:e.detail.value,
        })
    },
    //删除图片
    deleteImg: function (e) {
        var index = e.currentTarget.id;
        var that = this;
        var imageList = that.data.imageList;
        var delimg = imageList[index];
        //console.log(delimg);
        wx.request({
          url: Host + 'Mine/delfile',
          data: { imgurl: delimg},
          method: 'post',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          success: function (res) {
            //console.log(res.data);
            if (res.data.code == 200) {
              imageList.splice(index, 1);
              that.setData({
                imageList: imageList,
              })
            } else {
              wx.showToast({
                title: res.data.errmsg,
                icon: 'none',
              })
            }
          }
        })
    },
    //星评
    star:function(e){
        var star = parseInt(e.currentTarget.id)+1;
        this.setData({
            star: star,
        })
    },
    // 发布
    release:function(){
        var that=this;
        if(!that.data.textValue){
          wx.showToast({
            title: '请分享下您的观点哦',
            duration: 1500
          })
        }else{
          var orderid = that.data.orderid;
          var openid = wx.getStorageSync('openid');
          var star = that.data.star;
          var content = that.data.textValue
          var img = that.data.imageList;

          wx.request({
            url: Host + 'Mine/saveorderEvaluate',
            data: { orderid, img: JSON.stringify(img), star, content, openid },
            method: 'post',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              if (res.data.code == 200) {
                wx.showToast({
                  title: '发表成功！',
                  icon: 'loading',//  loading/success/none        
                  duration: 1000
                })
                setTimeout(function () {
                  wx.redirectTo({
                    url: '/pages/submission_should_get/submission_should_get?id=0',
                  })
                }, 1000)
              } else {
                wx.showToast({
                  title: '发表失败！',
                  icon: 'none',//  loading/success/none        
                  duration: 1000
                })
              }
            }
          });
        } 
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that=this;
      that.setData({
        orderid:options.id
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})